//
//  SettingWifiViewController.m
//  AlinkDemo
//
//  Created by Vincent on 2017/10/26.
//  Copyright © 2017年 aliyun. All rights reserved.
//

#import "SettingWifiViewController.h"
#import <SystemConfiguration/CaptiveNetwork.h>
#import "ScanQRViewController.h"

@interface SettingWifiViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *SSIDLabel;
@property (weak, nonatomic) IBOutlet UITextField *wifipwdTf;

- (IBAction)confirmAction:(id)sender;
@end

@implementation SettingWifiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.wifipwdTf.delegate = self;

    UIView*rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    UIButton *passButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [passButton setImage:[UIImage imageNamed:@"pwd_hide"] forState:UIControlStateNormal];
    [passButton setImage:[UIImage imageNamed:@"pwd_show"] forState:UIControlStateSelected];
    [passButton addTarget:self action:@selector(passButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0.5, 40)];
    line.backgroundColor = [UIColor lightGrayColor];
    [rightView addSubview:line];
    [rightView addSubview:passButton];
    self.wifipwdTf.rightView = rightView;
    self.wifipwdTf.rightViewMode = UITextFieldViewModeAlways;//一定要加上这句代码
    self.wifipwdTf.secureTextEntry = YES;
    self.wifipwdTf.delegate = self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.SSIDLabel.text = [self fetchSSIDInfo];
//    self.wifipwdTf.text = @"yugui151102";
}

- (void)passButtonAction:(UIButton *)button{
    button.selected = !button.selected;

    self.wifipwdTf.secureTextEntry = !self.wifipwdTf.secureTextEntry;
//    self.wifipwdTf.keyboardType = UIKeyboardTypeNumberPad;
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - wifi

- (id)fetchSSIDInfo {
    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    NSLog(@"Supported interfaces: %@", ifs);
    NSDictionary *info = nil;
    NSString *ssid = nil;
    for (NSString *ifnam in ifs) {
        info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
        NSLog(@"%@ => %@", ifnam, info);
        if (info && [info count]) {
            if ([info objectForKey:@"SSID"]) {
                ssid = [info objectForKey:@"SSID"];
            }
            break;
        }
    }
    //return info;
    return ssid;
}

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if ([segue.identifier isEqualToString:@"ScanQR"]) {
        ScanQRViewController *vc = [segue destinationViewController];
        vc.SSID = self.SSIDLabel.text;
        vc.wifipwd = self.wifipwdTf.text;
    }
}

- (IBAction)confirmAction:(id)sender {

    if (self.wifipwdTf.text.length==0||self.SSIDLabel.text.length==0) {

        return;
    }
    [self performSegueWithIdentifier:@"ScanQR" sender:nil];
}
@end
